<?php

namespace app\api\controller\User;

use think\Controller;
use think\Request;

class Address extends Controller
{
    public function __construct()
    {
        parent::__construct();

        $this->AddressModel = model('User.Address');
    }

    public function index()
    {
        // 接收用户id
        $userid = $this->request->param('userid');

        $addressList = $this->AddressModel->with(['provinces','citys','districts'])->where(['userid' => $userid])->select();

        if(!$addressList)
        {
            $this->error('暂无收货地址');
        }

        $this->success('查询成功',null,$addressList);
    }

    public function select()
    {
        $params = $this->request->param();

        $user = $this->AddressModel->where(['userid' => $params['userid']])->find();

        if(!$user)
        {
            $this->error('您不能修改他人的地址');
        }

        $address = $this->AddressModel->find($params['id']);

        if(!$address)
        {
            $this->error('该收货地址不存在');
        }

        // 这个用户的收货地址全部改为0再更新
        $status = $this->AddressModel->where(['userid' => $params['userid']])->update(['status' => 0]);

        $data = [
            'id' => $params['id'],
            'status' => 1
        ];

        $result = $this->AddressModel->isUpdate(true)->save($data);

        if($result === FALSE || $status === FALSE)
        {
            $this->error('修改默认收货地址失败');
        }else{
            $this->success('修改默认收货地址成功');
        }

    }
	
	// 编辑收货地址
	public function edit(){
		
		$params = $this->request->param();
		
		// 封装数组
		$data = [
			'id' => $params['id'],
			'consignee' => $params['consignee'],
			'mobile' => $params['mobile'],
			'address' => $params['address']
		];
		
		// 判断是否选中为默认地址
		if(!empty($params['status']) && $params['status'] == 1)
		{
			// 选中的话我就让该用户所有的收货地址状态改为0
			$this->AddressModel->where(['userid' => $params['userid']])->update(['status' => 0]);
			
			// 封装的数据中将其改为1
			$data['status'] = 1;
		}else{
			// 默认为0
			$data['status'] = 0;
		}
		
		if(!empty($params['code']))
		{
		    $parentpath = model('Region')->where(['code' => $params['code']])->value('parentpath');
		
		    if(!$parentpath)
		    {
		        $this->error('所选地区不存在');
		    }
		
		    [$province,$city,$district] = explode(',',$parentpath);
		
		    if($province)
		    {
		        $data['province'] = $province;
		    }
		
		    if($city)
		    {
		        $data['city'] = $city;
		    }
		
		    if($district)
		    {
		        $data['district'] = $district;
		    }
		}
		
		// 修改该条数据
		$result = $this->AddressModel->isUpdate(true)->save($data);
		
		if($result === FALSE)
		{
			$this->error('修改失败');
		}else{
			$this->success('修改成功','/user/address/index');
		}
		
	}
	// 添加
    public function add()
    {
        $params = $this->request->param();

        $data = [
            'consignee' => $params['consignee'],
            'mobile' => $params['mobile'],
            'address' => $params['address'],
            'userid' => $params['userid']
        ];

        // 设置默认
        if(!empty($params['status']) && $params['status'] == 1)
        {
            $this->AddressModel->where(['userid' => $params['userid']])->update(['status' => 0]);

            $data['status'] = $params['status'];
        }

        if(!empty($params['code']))
        {
            $parentpath = model('Region')->where(['code' => $params['code']])->value('parentpath');

            if(!$parentpath)
            {
                $this->error('所选地区不存在');
            }

            [$province,$city,$district] = explode(',',$parentpath);

            if($province)
            {
                $data['province'] = $province;
            }

            if($city)
            {
                $data['city'] = $city;
            }

            if($district)
            {
                $data['district'] = $district;
            }
        }

        $result = $this->AddressModel->validate('common/User/Address')->save($data);

        if($result === FALSE)
        {
            $this->error('新增失败');
        }else{
            $this->success('新增成功','/user/address/index');
        }
    }

    public function info()
    {
        $addrid = $this->request->param('addrid');

        $address = $this->AddressModel->with(['provinces','citys','districts'])->find($addrid);

        if(!$address)
        {
            $this->error('该收货地址不存在');
        }

        $this->success('查询成功',null,$address);
    }
	
	// 删除收货地址
	public function addressdel(){
		
		$addrid = $this->request->param('addrid');
		 
		$address = $this->AddressModel->find($addrid);
		
		if(!$address)
		{
			$this->error('您删除的收货地址不存在');
		}
		
		// 删除
		$result = $this->AddressModel->where('id',$addrid)->delete();
		
		if($result === FALSE)
		{
			$this->error('删除失败');
		}else{
			$this->success('删除成功','/user/address/index');
		}
		
	}
	
	 public function order()
	{
		$userid = $this->request->param('userid');

		$address = model('User.Address')->where(['userid' => $userid,'status' => 1])->find();

		if(!$address)
		{
			$this->error('暂无默认收货地址');
		}

		$this->success('查询成功',null,$address);
	}

}
